/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fasea.ui.properties;

import fasea.constants.Constants;
import fasea.model.Action;
import fasea.ui.AbstractController;
import fasea.utils.FileUtil;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author lars-wolfram
 */
public class PropertiesController extends AbstractController<PropertiesView> {

    private void view() {
        if (model.getLastIndexed() == 0) {
            view.setLastIndexed("Never");
            view.setIndexAlertIcon();
        } else if(new Date().getTime() - model.getLastIndexed() > Constants.getIndexExpirationTime()){
            view.setLastIndexed(dateToString(model.getLastIndexed()));
            view.setIndexAlertIcon();
        }else{
            view.setLastIndexed(dateToString(model.getLastIndexed()));
            view.setIndexOKIcon();
        }
        view.setIndexDirectory(model.getIndexFolder());
        view.setIndexingFolders(model.getIndexingFolders());
        view.setIndexSize(FileUtil.getFileSize(model.getIndexFolder()));
        view.setIndexedDocumentsCount(model.getIndexerDocumentsCount());
        view.setVisible(true);
    }
    
    private synchronized String dateToString(long millis) {
        String lastIndexedString = "";
        if (millis == 0) {
            lastIndexedString = "never";
        } else {
            DateFormat formatter = new SimpleDateFormat();
            lastIndexedString = formatter.format(new Date(millis));
        }

        return lastIndexedString;
    }

    @Override
    public void update(Action action) {
        switch(action){
            case INDEXER_ABORTED:
                view.setIndexAlertIcon();
                view.enableStartButton(true);
                break;
            case INDEXER_FINISHED:
                 view.setIndexOKIcon();
                 view.enableStartButton(true);
                break;
            case INDEXER_STARTED:
                view.setIndexRunningIcon();
                 view.enableStartButton(false);
                break;
            case INDEXER_DOCUMENTSCOUNT_CHANGED:
                int documentsCount = model.getIndexerDocumentsCount();
                view.setIndexedDocumentsCount(documentsCount);
                break;
           case INDEXER_INFOTEXT_CHANGED:
                String info = model.getIndexerInfoText();
                view.setIndexerInfoText(info);
                break;
           case SHOW_PROPERTIESVIEW:
                view();
                break;
        }
    }

    @Override
    protected void initView() {
       this.view.addStartButtonActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                model.setIndexingFolders(PropertiesController.this.view.getIndexingFolders());
                model.saveIndexProperties();
                model.startIndexing();
            }
        });
        this.view.addStopButtonActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                model.abortIndexing();
            }
        });
        this.view.addOkButtonActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent ae) {
                model.setIndexingFolders(PropertiesController.this.view.getIndexingFolders());
                model.saveIndexProperties();
                PropertiesController.this.view.setVisible(false);
            }
        });
    }
}
